package com.tansun.easycare.rule.globalsearch.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.globalsearch.entity.RuleGlobalSearch;
import com.tansun.easycare.rule.globalsearch.service.RuleGlobalSearchService;
import com.tansun.rule.common.RuleModelTypeEnum;

@Controller
//@RequestMapping(value = "${adminPath}/globalSearch/ruleGlobalSearch")
@RequestMapping(value = "/ruleProj/ruleGlobalSearch/")
public class RuleGlobalSearchController extends RuleBaseController {

    @Autowired
    private RuleGlobalSearchService ruleGlobalSearchService;


    /**
     * 规则查询列表
     */
//    @RequiresPermissions("rule:globalSearch:view")
	@RequestMapping(value = {"index","list", ""})
    public String list(RuleGlobalSearch ruleGlobalSearch,HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {           	
		ruleGlobalSearch.getPage().setPageSize(20);
 
    	List<RuleGlobalSearch> globalSearchList = new ArrayList<RuleGlobalSearch>();
    	if("byParams".equals(ruleGlobalSearch.getSearchType())) {
    		globalSearchList= ruleGlobalSearchService.getRuleGlobalSearchListByParams(ruleGlobalSearch);
    	}else if("byRule".equals(ruleGlobalSearch.getSearchType())){
    		globalSearchList= ruleGlobalSearchService.getRuleGlobalSearchListByRule(ruleGlobalSearch);
    	}else if("byModel".equals(ruleGlobalSearch.getSearchType())) {
    		globalSearchList= ruleGlobalSearchService.getRuleGlobalSearchListByModel(ruleGlobalSearch);
    	}
    	for(RuleGlobalSearch rgs : globalSearchList) {//转换规则类型
    		String ModelTypeCn = RuleModelTypeEnum.getName(rgs.getModelType());
    		rgs.setModelType(ModelTypeCn);
    	}
    	PageInfo<RuleGlobalSearch> pageInfo = new PageInfo<>(globalSearchList);
    	model.addAttribute("page", pageInfo);
    	model.addAttribute("projectCode", ruleGlobalSearch.getProjectCode());
    	model.addAttribute("ruleCheckbox", ruleGlobalSearch.getRuleCheckbox());
    	model.addAttribute("deriveVariablesCheckbox", ruleGlobalSearch.getDeriveVariablesCheckbox());
    	model.addAttribute("modelCheckbox", ruleGlobalSearch.getModelCheckbox());
    	model.addAttribute("decisionFlowCheckbox", ruleGlobalSearch.getDecisionFlowCheckbox());
        return "rule/globalSearch/globalSearchList";
    }

   
}
